package com.ideal.util.file;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.util.*;

/**
 * @Author songfz
 * @Description
 * @Date 2022/7/27
 */
public class PropertiesUtil {

    private static Logger LOG = Logger.getLogger(PropertiesUtil.class);

    /**
     * 读取properties配置文件
     * @param path
     * @return
     */
    public static String readContext(String path){


        final HashMap<String, String> contextMap = new HashMap<>(16);
        Properties properties = new Properties();
        try(FileInputStream fis = new FileInputStream(path)){
            properties.load(fis);
            final Enumeration<?> e = properties.propertyNames();
            while (e.hasMoreElements()) {
                String key =(String) e.nextElement();
                 String value = properties.getProperty(key);

                key = new String(key.getBytes("ISO8859-1"),"utf-8");
                value = new String(value.getBytes("ISO8859-1"),"utf-8");

                contextMap.put(key, value);

            }
           return JSON.toJSONString(contextMap);
        }catch (Exception e){
            LOG.error(e.getMessage(),e);
        }
        return null;

    }

    /**
     * properties文件内容反解析
     * @param context
     * @return
     */
    public static String parse(String context){
        if (!StringUtils.hasLength(context)) {
            return null;
        }
        final StringBuffer sb = new StringBuffer();
        final HashMap<String, String> contextMap = JSON.parseObject(context, HashMap.class);

        for (Map.Entry<String, String> entry : contextMap.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\r\n");
        }
        return sb.toString();
    }


    /**
     * 文件文本内容解析成json数据
     * @param fileContext
     * @return
     */
    public static String textToContext(String fileContext){

        if (!StringUtils.hasLength(fileContext)) {
            throw new RuntimeException("数据异常");
        }
        LinkedHashMap<String, String> contextMap = new LinkedHashMap<>();
        final String[] contexts = fileContext.split("\r\n");

        for (String context : contexts) {
            final String[] split = context.split("=");
            String key = split[0];
            String value = split[1];
            contextMap.put(key, value);
        }
        return JSON.toJSONString(contextMap);
    }

    public static void main(String[] args) {
        String url = "F:\\pdf\\propeirtis文件.properties";

        final String context = readContext(url);
        System.out.println(context);


        final String parse = parse(context);
        System.out.println(parse);

        final String contexts = textToContext(parse);

        System.out.println(contexts);
    }

    public static Map<String, String> strToJson(String content) {
        return JSON.parseObject(content, HashMap.class);
    }
}
